let { User, Message, Comment, administrators, topic, questionnaire ,fraction} = require('../models');
let auth = require('../utils/auth');

let login_fn = async (ctx, next) => {
    ctx.render('login.html');
}
let register_fn = async (ctx, next) => {
    ctx.render('register.html')
}
let indexyh_fn = async (ctx, next) => {
    var users = JSON.parse(JSON.stringify(await questionnaire.findAll()));
    var obj = {
        users: users,
    }
    ctx.render('indexyh.html', obj)
}


// let grade_fn = async (ctx, next) => {
//     ctx.render('grade.html')
// }
let loginDone_fn = async (ctx, next) => {
    let username = ctx.request.body.username || '';
    let password = ctx.request.body.password || '';
    let res;
    let u1 = await User.findOne({
        where: {
            username: username,
            password: password
        }
    }).then((row) => {
        let user = JSON.stringify(row);
        let u2 = JSON.parse(user);

        if (user !== 'null') {
            ctx.session.userId = row.id;
            ctx.session.userName = row.username;
            res = { code: 200, msg: '登录成功' };
        } else {
            res = { code: 1000, msg: '用户名或密码错误，请重试' };
        }
    });
    ctx.body = res;
}
let registerDone_fn = async (ctx, next) => {
    let username = ctx.request.body.username || '';
    let password = ctx.request.body.password || '';
    let confirmpassword = ctx.request.body.confirmpassword || '';

    if (username.length > 0 && password.length > 0 && confirmpassword.length > 0 && password === confirmpassword) {
        let userDemo = await User.findOne({ where: { username: username } });
        console.log(JSON.stringify(userDemo));
        let res = '';
        if (userDemo) {
            res = { code: 1000, msg: '当前用户名已经注册，请确认后重试' };
        } else {
            let u1 = User.create({
                username: username,
                password: password
            });
            res = { code: 200, msg: '注册成功' };

        }
        ctx.body = JSON.stringify(res);
    } else {
        console.log('用户名或密码不能为空；并且两次密码应该相同');
    }
}
let say_fn = async (ctx, next) => {
    let msgSay = ctx.request.body.msgSay || '';
    let userId = ctx.session.userId;
    let msg1 = await Message.create({
        fromUserId: userId,
        content: msgSay
    });
    let data = JSON.stringify(msg1)

    ctx.body = { code: 200, data, msg: '发表成功' };
}
let comment_fn = async (ctx, next) => {
    let msgId = ctx.request.body.msgId || '';
    let userId = ctx.session.userId;
    let commentText = ctx.request.body.content || '';
    let msg1 = await Comment.create({
        fromUserId: userId,
        msgId: msgId,
        content: commentText
    });
    let fromUser = await User.findOne({
        where: {
            id: userId
        }
    });
    let msg = await Message.findOne({
        where: {
            id: msgId
        }
    });
    let toUser = await User.findOne({
        where: {
            id: msg.fromUserId
        }
    });
    let data = JSON.parse(JSON.stringify(msg1));

    data.fromUserName = fromUser.username;
    data.toUserName = toUser.username;

    ctx.body = { code: 200, data, msg: '发表成功' };
}

let logout_fn = async (ctx, next) => {
    ctx.session = null;
    ctx.body = { code: 200, msg: '注销成功！' }

}
let username_fn = async (ctx, next) => {
    var users = JSON.parse(JSON.stringify(await User.findAll()));
    var obj = {
        users: users,
    }
    // var topic = JSON.parse(JSON.stringify(await topic.findAll()));
    // var obj = {
    //     topic: topic,
    // }
    ctx.render('username.html', obj)
}
let delect_fn = async (ctx, next) => {
    let id = ctx.request.body.userId || '';
    console.log(id);
    let de = await User.destroy({
        where: {
            id: id
        }
    })
    res = { code: 200, msg: '删除成功' };
    ctx.body = JSON.stringify(res);
}
let lookup_fn = async (ctx, next) => {
    let id = ctx.request.body.userId || '';
    let username = ctx.request.body.lookupusername || '';
    let password = ctx.request.body.lookuppassword || '';
    console.log(id);
    let de = await User.update({ username: username, password: password }, {
        where: {
            id: id
        }
    })
    res = { code: 200, msg: '编辑成功' };
    ctx.body = JSON.stringify(res);
}
let fabuhengjuan_fn = async (ctx, next) => {
    let title = ctx.request.body.title || '';
    let theme = ctx.request.body.theme || '';
    let topic01 = ctx.request.body.topic01 || '';
    let topic02 = ctx.request.body.topic02 || '';
    let topic03 = ctx.request.body.topic03 || '';
    let topic04 = ctx.request.body.topic04 || '';
    let topic05 = ctx.request.body.topic05 || '';
    let topic06 = ctx.request.body.topic06 || '';
    let topic07 = ctx.request.body.topic07 || '';
    let topic08 = ctx.request.body.topic08 || '';
    let topic09 = ctx.request.body.topic09 || '';
    let topic10 = ctx.request.body.topic10 || '';
    let titleDemo = await questionnaire.findOne({ where: { title: title } });
    console.log(JSON.stringify(titleDemo));
    let u1 = questionnaire.create({
        title: title,
        theme: theme
    });
    let topicDemo = await topic.findOne({ where: { topic01: topic01 } });
    console.log(JSON.stringify(topicDemo));
    let u2 = topic.create({
        topic01: topic01,
        topic02: topic02,
        topic03: topic03,
        topic04: topic04,
        topic05: topic05,
        topic06: topic06,
        topic07: topic07,
        topic08: topic08,
        topic09: topic09,
        topic10: topic10
    });

    res = { code: 200, msg: '发布成功' };
    ctx.body = JSON.stringify(res);

}
let publish_fn = async (ctx, next) => {
    var users = JSON.parse(JSON.stringify(await questionnaire.findAll()));
    var obj = {
        users: users,
    }
    ctx.render('publish.html', obj)

}
let details_fn = async (ctx, next) => {
    let msgId = ctx.request.body.msgId;

    let u1 = JSON.parse(JSON.stringify(await topic.findOne({
        where: {
            id: msgId
        }
    })));
    console.log(u1);
    ctx.body = { code: 200, msg: u1 }
}
let tijiao_fn = async (ctx, next) => {
    let tijiao01 = ctx.request.body.tijiao01 || '';
    let tijiao02 = ctx.request.body.tijiao02 || '';
    let tijiao03 = ctx.request.body.tijiao03 || '';
    let tijiao04 = ctx.request.body.tijiao04 || '';
    let tijiao05 = ctx.request.body.tijiao05 || '';
    let tijiao06 = ctx.request.body.tijiao06 || '';
    let tijiao07 = ctx.request.body.tijiao07 || '';
    let tijiao08 = ctx.request.body.tijiao08 || '';
    let tijiao09 = ctx.request.body.tijiao09 || '';
    let tijiao10 = ctx.request.body.tijiao10 || '';
    let tijiaoDemo = await fraction.findOne({ where: { tijiao01: tijiao01 } });
    let u2 = fraction.create({
        tijiao01: tijiao01,
        tijiao02: tijiao02,
        tijiao03: tijiao03,
        tijiao04: tijiao04,
        tijiao05: tijiao05,
        tijiao06: tijiao06,
        tijiao07: tijiao07,
        tijiao08: tijiao08,
        tijiao09: tijiao09,
        tijiao10: tijiao10
    });
    console.log(u2);
    res = { code: 200, msg: '提交成功' };
    ctx.body = JSON.stringify(res);

}

let grade_fn = async (ctx, next) => {
    // let u1 = JSON.parse(JSON.stringify(await topic.findAll({
    //     where: {
    //         u1: u1
    //     }
    // })));
    // let u2 = JSON.parse(JSON.stringify(await User.findAll({
    //     where: {
    //         u2: u2
    //     }
    // })));
    // let u3 = JSON.parse(JSON.stringify(await fraction.findAll({
    //     where: {
    //         u3: u3
    //     }
    // })));
    // console.log(u1);
    // ctx.body = { code: 200, msg1:u1 ,msg2:u2,msg3:u3}
    ctx.render('grade.html')
}
// let grade02_fn = async (ctx, next) => {
//     var topic = JSON.parse(JSON.stringify(await topic.findAll()));
//     var obj = {
//         topic: topic,
//     }
//     ctx.render('grade.html', obj)
// }
// let grade03_fn = async (ctx, next) => {
//     var fraction = JSON.parse(JSON.stringify(await fraction.findAll()));
//     var obj = {
//         fraction: fraction,
//     }
//     ctx.render('grade.html', obj)
// }
module.exports = {
    '/indexyh': ['get', auth, indexyh_fn],
    '/login': ['get', auth, login_fn],
    '/register': ['get', register_fn],
    '/loginDone': ['post', loginDone_fn],
    '/registerDone': ['post', registerDone_fn],
    '/testDone': ['post', say_fn],
    '/comment': ['post', comment_fn],
    '/logout': ['post', logout_fn],
    '/delect': ['post', delect_fn],
    '/lookup': ['post', lookup_fn],
    '/fabuhengjuan': ['post', fabuhengjuan_fn],
    '/tijiao': ['post', tijiao_fn],
    '/say': ['post', say_fn],
    '/username': ['get', username_fn],
    '/': ['get', publish_fn],
    '/grade': ['get', grade_fn],
    // '/grade': ['get', grade02_fn],
    // '/grade': ['get', grade03_fn],
    '/details': ['post', details_fn],
}